<?php
require_once dirname(dirname(__FILE__)) . '/shared/config/config.php';
require_once DOCUMENT_ROOT . DIR_INCLUDE . 'check_user_login.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_security.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_roulette.php';
require_once DOCUMENT_ROOT . DIR_INCLUDE . 'html_generator.php';
$title = 'Roulette Detail';

//FORM INPUT------------------------------
$txt_name = filter_input(INPUT_POST, 'txt_name');
$txt_start_date = filter_input(INPUT_POST, 'txt_start_date');
$txt_end_date = filter_input(INPUT_POST, 'txt_end_date');
$hidden_id = filter_input(INPUT_POST, 'hidden_id');
$btn_save = filter_input(INPUT_POST, 'btn_save');
$edit_item = NULL;

//INSERT or UPDATE
if (!empty($btn_save)) {
    //INSERT
    if (empty($hidden_id)) {
        if (api_roulette::insert_roulette($txt_name, $txt_start_date, $txt_end_date)) {
            ?>
            <script type="text/javascript">
                alert('Insert Successfull');
                window.location = '<?php echo $_SERVER['PHP_SELF'] ?>';
            </script>
        <?php
        } else {
            ?>
            <script type="text/javascript">
                alert('Insert Failed');
                window.location = '<?php echo $_SERVER['PHP_SELF'] ?>';
            </script>
        <?php
        }
    }

    //UPDATE
    if (!empty($hidden_id)) {
        if (api_roulette::update_roulette($hidden_id, $txt_name, $txt_start_date, $txt_end_date)) {
            ?>
            <script type="text/javascript">
                alert('Update Successfull');
                window.location = '<?php echo $_SERVER['PHP_SELF'] ?>';
            </script>
        <?php
        } else {
            ?>
            <script type="text/javascript">
                alert('Update Failed');
                window.location = '<?php echo $_SERVER['PHP_SELF'] ?>';
            </script>
        <?php
        }
    }
}


//FORM TABLE--------------------------------
$id_edit = filter_input(INPUT_POST, 'btn_id_edit');
$id_delete = filter_input(INPUT_POST, 'btn_id_delete');

//GET EDIT ITEM
if (!empty($id_edit)) {
    $edit_item = api_roulette::get_roulette_by_id($id_edit);
}

//DELETE
if (!empty($id_delete)) {
    if (api_roulette::delete_roulette($id_delete)) {
        ?>
        <script type="text/javascript">
            alert('Detele Successfully');
            window.location = '<?php echo $_SERVER['PHP_SELF'] ?>';
        </script>
    <?php
    } else {
        ?>
        <script type="text/javascript">
            alert('Detele Failed');
            window.location = '<?php echo $_SERVER['PHP_SELF'] ?>';
        </script>
    <?php
    }
}

$list = api_roulette::get_all_roulettes();
?>

<!DOCTYPE html>
<!-- IE SECTION -->

<!--[if lt IE 7]>
<html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>
<html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>
<html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js"> <!--<![endif]-->

                     <!-- END IE SECTION -->
<html>
<head>
    <?php require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'head.php' ?>
    <link rel="stylesheet" type="text/css" href="<?php echo LINK_ROOT . DIR_SHARED_LAYOUT_CSS . 'jquery.datetimepicker.css' ?>"/>
</head>
<body>
<?php require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'header.php' ?>

<!-- Title -->
<div class="section section-black">
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <div class="calltoaction-wrapper">
                    <h3>Roulette Detail</h3>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- End Title -->

<!-- Match Detail -->
<div class="section">
    <div class="container">
        <div class="row">

            <div class="col-md-offset-1 col-md-10">
                <div class="service-wrapper row">

                    <form class="form-horizontal" role="form" method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">

                        <input type="hidden" name="hidden_id" value="<?php echo $edit_item['id'] ?>"/>

                        <div class="col-md-3">
                            <img class="text-left" src="<?php echo LINK_ROOT . DIR_SHARED_LAYOUT_IMAGES . 'roulette/map.png' ?>" alt="Match" style="height: 500px;">
                        </div>
                        <div class="col-md-offset-1 col-md-8">

                            <div class="form-group ts-margin-top">
                                <label class="col-sm-4 control-label">Name</label>

                                <div class="col-sm-7 text-left">
                                    <input type="text" name="txt_name" value="<?php echo $edit_item['name'] ?>" placeholder="name" class="form-control">
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-sm-4 control-label">Start Date</label>

                                <div class="col-sm-7 text-left">
                                    <input type="text" name="txt_start_date" value="<?php echo $edit_item['date_start']; ?>" placeholder="2014/06/13 00:00" class="form-control" id="txt_start_date">
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-sm-4 control-label">End Date</label>

                                <div class="col-sm-7 text-left">
                                    <input type="text" name="txt_end_date" value="<?php echo $edit_item['date_end']; ?>" placeholder="2014/06/14 00:00" class="form-control" id="txt_end_date">
                                </div>
                            </div>

                            <input name="btn_save" type="submit" class="btn" value="Save">

                        </div>
                    </form>

                </div>
                <!--End service-wrapper-->
            </div>
        </div>
        <!--End row-->
    </div>
    <!--End container-->
</div>
<!-- End Section Form -->

<!-- Section Table -->
<div class="container">
    <div class="row">
        <div class="col-md-offset-1 col-md-10">
            <div class="service-wrapper">
                <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST">
                    <table class="table">
                        <tr>
                            <th class="text-center">ID</th>
                            <th class="text-center">Name</th>
                            <th class="text-center">Start Date</th>
                            <th class="text-center">End Date</th>
                            <th class="text-center" colspan="2">Action</th>
                        </tr>
                        <?php
                        if (!empty($list)) :
                            foreach ($list as $item) : ?>
                                <tr>
                                    <td class="text-center"><?php echo $item['id'] ?></td>
                                    <td class="text-center"><?php echo $item['name'] ?></td>
                                    <td class="text-center"><?php echo $item['date_start'] ?></td>
                                    <td class="text-center"><?php echo $item['date_end'] ?></td>
                                    <td>
                                        <button type="submit" name="btn_id_edit" style="background: none; border: 0" value="<?php echo $item['id'] ?>">
                                            <img class="ts-action-icon center-block"
                                                 src="<?php echo LINK_ROOT . DIR_SHARED_LAYOUT_IMAGES_ICONS . 'edit.png' ?>"
                                                 alt="edit">
                                        </button>
                                    </td>
                                    <td>
                                        <button type="submit" name="btn_id_delete" style="background: none; border: 0" value="<?php echo $item['id'] ?>" onclick="return confirm('Are you sure?')">
                                            <img class="ts-action-icon center-block"
                                                 src="<?php echo LINK_ROOT . DIR_SHARED_LAYOUT_IMAGES_ICONS . 'delete.png' ?>"
                                                 alt="edit">
                                        </button>
                                    </td>
                                </tr>
                            <?php endforeach;
                        else :
                            ?>
                            <tr>
                                <td colspan="6" class="text-center text-danger">EMPTY LIST</td>
                            </tr>
                        <?php endif; ?>
                    </table>
                </form>
            </div>
        </div>
    </div>
</div>
<!-- End Section Table -->

<script type="text/javascript" src="<?php echo LINK_ROOT . DIR_SHARED_LAYOUT_JS . 'jquery.datetimepicker.js' ?>"></script>
<script type="text/javascript">
    $('#txt_start_date').datetimepicker()
        .datetimepicker({value: '', step: 10});
    $('#txt_end_date').datetimepicker()
        .datetimepicker({value: '', step: 10});
</script>

<?php require_once DOCUMENT_ROOT . DIR_INCLUDE . 'footer.php' ?>
</body>
</html>